var fs = require('fs')
const { parse } = require('path')
/*获取全部数据 */
exports.findAll = function (callback) {
    fs.readFile("db.json", function (err, data) {
        if (err) {
            return callback(err)
        } else {
            callback(null, data)
        }
    })
}
/*把新加的学生数据保存到文件内 */
exports.save = function (newstudent, callback) {
    fs.readFile("db.json", 'utf8', function (err, data) {
        if (err) {
            callback(err)
        } else {
            var students = JSON.parse(data).students;
            newstudent.id = students.length + 1;
            students.push(newstudent)
            //callback(null, data)
            var feilData = JSON.stringify({
                students: students
            })
            console.log(feilData);
            fs.writeFile('db.json', feilData, function (err) {
                if (err) {
                    return callback(err)
                } else {
                    callback(null)
                }
            })
        }
    })
}
/*查找id的信息*/
exports.findById = function (id, callback) {
    fs.readFile("db.json", function (err, data) {
        if (err) {
            return callback(err)
        } else {
            console.log(id);
            var IDData = JSON.parse(data).students;
            var Searchindex = IDData.findIndex(it => it.id === id);
            console.log('seaindex', Searchindex);
            var findIDData = IDData[Searchindex]
            callback(null, JSON.stringify(findIDData))
        }
    })
}
/* 修改学生 */
exports.updata = function (student, callback) {
    fs.readFile("db.json", function (err, data) {
        if (err) {
            return callback(err)
        } else {
            //callback(null, data)
            var students = JSON.parse(data).students
            var findstu = students.find(function (item) {
                return parseInt(item.id) === parseInt(student.id)
            });
            console.log(student);
            console.log(findstu);
            for (var key in student) {
                findstu[key] = student[key];
            }
            console.log(students);
            fs.writeFile("db.json", JSON.stringify({
                students: students
            }), function (err) {
                if (err) {
                    callback(err)
                } else {
                    callback(null)
                }
            })
        }
    })
}
/* 删除学生 */
exports.delete = function (student, callback) {
    fs.readFile('db.json', function (err, data) {
        if (err) {
            return callback(err)
        } else {
            console.log(student);
            var students = JSON.parse(data).students;
            console.log('length', students.length);
            /*  if (students.length != 1) { */
            var Deleteindex = students.findIndex(it => it.id === student.id);
            console.log(Deleteindex);
            if (Deleteindex !== -1) {
                console.log(Deleteindex);
                students.splice(Deleteindex, 1);
                console.log(students);
                fs.writeFile('db.json', JSON.stringify({
                    students: students
                }), function (err) {
                    if (err) {
                        callback(err)
                    } else {
                        callback(null)
                    }
                })
            } else {
                callback(err)
            }
            /*} else {
                students[0].id = 1;
                console.log(students);
                fs.writeFile('db.json', JSON.stringify({
                    students: students
                }), function (err) {
                    if (err) {
                        callback(err)
                    } else {
                        callback(null)
                    }
                })
                var Deleteindex = students.findIndex(it => it.id === student.id);
                console.log(Deleteindex);
                if (Deleteindex !== -1) {
                    console.log(Deleteindex);
                    students.splice(Deleteindex, 1);
                    console.log(students);
                    fs.writeFile('db.json', JSON.stringify({
                        students: students
                    }), function (err) {
                        if (err) {
                            callback(err)
                        } else {
                            callback(null)
                        }
                    })
                } else {
                    callback(err)
                } 
        }*/

        }
    })

}